<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;
use DB;
use Crypt;
class UserController extends Controller
{
	//用户注册
    public function register()
    {
        return view('home.user.register');
    }

    //ajaxEmail
    public function ajaxEmail(Request $request)
    {

    	$code = rand(000000, 999999);//取随机数
    	$request -> code = $code;
    	session(['code' => $code]);//将验证码存入session

    	$sus = Mail::send('emails.checkCode', ['request' => $request], function($message) use ($request){
                $message -> subject('天下美食邮箱注册验证码');
                $message -> to($request -> email);//发送的邮箱
            });

            if($sus)
	 		{
	 			return response() -> json(0);
	 		}else
	 		{
	 			return response() -> json(1);
	 		}

    }

    //ajax判断邮箱用户是否存在
    //emailCheck
	public function emailCheck(Request $request)
	{
	 	$email = $request -> input('email');
	 	//注:这个name是post传过来的;
	 	//查询
	 	$res = DB::table('businesses') -> where('email', $email) -> first();


	 	if($res)
	 	{
	 		//1代表用户名已经存在
	 		return response() -> json(1);
	 	}else
	 	{

	 		return response() -> json(0);
	 	}
	}

    //add
    public function add(Request $request)
    {
       //判断邮箱验证码
      if(session('code')  == $request -> verify)
        {
            // //处理好数据
            $data = $request -> except('_token', 'verify');


            //加密密码
            $data['password'] = Crypt::encrypt($data['password']);
            // dd($data);

            $data['remember_token'] = str_random(70);
            $data['username']=str_random(10);
            $time = date("Y-m-d H:i:s");
            $data['created_at'] = $time;
            $data['updated_at'] = $time;

            //添加数据
            $res = DB::table('users') -> insert($data);
            if($res)
            {
                return redirect('login') -> with(['info' => '注册成功,请登录']);

            }else
            {
                return redirect('home/user/regist') -> with(['info' => '登录失败']);
            }

        }else
        {
            return redirect('home/user/register') -> with(['info' => '验证码不匹配']);
        }

    }

    //登录
    public function login()
    {
        return view('home.user.login');
    }


    //dologin
    public function dologin(Request $request)
    {
        //处理数据
        $data = $request -> except('_token');

        //验证码是否正确
        if($data['captcha_code'] == session('milkcaptcha'))
            {
                $res = DB::table('users')-> where('email', $data['email']) -> first();
                $pass = $res -> password;//数据库的密码
                $password = Crypt::decrypt($pass);

            if($data['password'] == $password)
                {
                    session(['master' => $res]);//将数据存入session,传到前台
                    // dd(session() -> all());
                    return redirect($request->session()->get('maio_url')) -> with(['info' => '登录成功']);
                }


        }else
        {
            return back() -> with(['info' => '验证码错误']);
        }
    }




}
